<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    

          <script>
               
                //被观察者
                 class subject{
                     constructor(name){
                         this.name=name
                         this.setate='开心'
                         this.subArr=[]
                     }
                     attach(obj){
                        this.subArr.push(obj)
                     }
                     setState(status){
                        this.setate=status
                        this.subArr.forEach((item)=>{
                            item.know(this)
                        })
                     }


                 }
                //观察者
                 class Parents{
                     constructor(name){
                         this.name=name
                     }
                     know(data){
                         console.log(this.name+'知道'+data.name+data.setate)
                     }

                 }
                 let bb=new subject('xiaobaobao')
                 let fa=new Parents('father')
                 let ma=new Parents('mother')

                 bb.attach(fa)
                 bb.attach(ma)
                 bb.setState('不开心')

                 console.log(bb)
                 console.log(fa)
                 console.log(ma)

                    




          </script>
 

</body>
</html>